package org.lyq.erp.process.users.controller;


import java.util.List;
import javax.annotation.Resource;
import org.lyq.erp.process.userrank.pojo.TblWxUserrank;
import org.lyq.erp.process.userrank.service.TblWxUserrankService;
import org.lyq.erp.process.users.pojo.TblWxUsers;
import org.lyq.erp.process.users.pojo.UsersUpdateVo;
import org.lyq.erp.process.users.service.TblWxUsersService;
import org.lyq.erp.utils.string.ObjectEmptyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;




@Controller
@RequestMapping({"/TblWxUsers"})
public class TblWxUsersController
{
  private static Logger logger = LoggerFactory.getLogger(TblWxUsersController.class);
  
  @Resource
  private TblWxUsersService tblWxUsersService;
  
  @Resource
  private TblWxUserrankService tblWxUserrankService;

  
  @RequestMapping({"/list"})
  public String list(ModelMap map) {
    List<TblWxUserrank> userrankList = this.tblWxUserrankService.list();
    map.put("userrankList", userrankList);
    List<TblWxUsers> usersList = this.tblWxUsersService.selectAllUsers();
    map.put("usersList", usersList);
    return "process/users/users_list";
  }
  
  @RequestMapping({"/updateUsers"})
  public String updateUsers(UsersUpdateVo usersUpdateVo, ModelMap map, String menuUuid) {
    if (usersUpdateVo.getUserId() != null) {
      if (!ObjectEmptyUtils.isEmptyString(usersUpdateVo.getReduce()) && usersUpdateVo
        .getAddUserTatolscore() != null && usersUpdateVo.getUserTatolscore() != null) {
        if ("0".equals(usersUpdateVo.getReduce())) {
          
          if (usersUpdateVo.getAddUserTatolscore().intValue() > usersUpdateVo.getUserTatolscore().intValue()) {
            
            usersUpdateVo.addErrorMessage("users001");
            errorPageBackDate(usersUpdateVo, map);
            return "process/users/users_list";
          } 
          usersUpdateVo.setUserTatolscore(
              Integer.valueOf(usersUpdateVo.getUserTatolscore().intValue() - usersUpdateVo.getAddUserTatolscore().intValue()));
        } else {
          
          usersUpdateVo.setUserTatolscore(
              Integer.valueOf(usersUpdateVo.getUserTatolscore().intValue() + usersUpdateVo.getAddUserTatolscore().intValue()));
        } 
      }
      try {
        this.tblWxUsersService.updateUsers(usersUpdateVo);
      } catch (Exception e) {
        logger.info(e.getMessage());
        usersUpdateVo.addErrorMessage("users002");
        errorPageBackDate(usersUpdateVo, map);
        return "process/users/users_list";
      } 
    } else {
      errorPageBackDate(usersUpdateVo, map);
      return "process/users/users_list";
    } 
    return "redirect:/TblWxUsers/selectOneUsers?userId=" + usersUpdateVo.getUserId() + "&menuUuid" + menuUuid;
  }


  
  @RequestMapping({"/selectOneUsers"})
  public String selectOneUsers(ModelMap map, String userId) {
    List<TblWxUserrank> userrankList = this.tblWxUserrankService.list();
    map.put("userrankList", userrankList);
    List<TblWxUsers> usersList = this.tblWxUsersService.selectByPrimaryKey(userId);
    map.put("usersList", usersList);
    return "process/users/users_list";
  }


  
  @RequestMapping({"/gotoUserDetail"})
  public String gotoUserDetail(String userId) { return "process/users/users_detail"; }

  
  private void errorPageBackDate(UsersUpdateVo usersUpdateVo, ModelMap map) {
    usersUpdateVo.popuperrorMessage = "true";
    map.put("dataBean", usersUpdateVo);
    List<TblWxUserrank> userrankList = this.tblWxUserrankService.list();
    map.put("userrankList", userrankList);
    List<TblWxUsers> usersList = this.tblWxUsersService.selectAllUsers();
    map.put("usersList", usersList);
  }
}
